package com.example.tranhtheuchuthap;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


public class TraBangMau extends Activity {
	 String mamauDMC="",tenMau="";
	   String Red="",Green="",Blue="";
	   EditText edt_Red, edt_Green, edt_Blue, edt_maDMC, edt_tenMau;
	   TextView tv_R,tv_G,tv_B;
	   ImageView imv_mau;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tra_bang_mau);
		 imv_mau=(ImageView)findViewById(R.id.iv_hien_thi_mau);
	 edt_Red=(EditText)findViewById(R.id.ed_Red);
		 edt_Green=(EditText)findViewById(R.id.ed_Green);
		 edt_Blue=(EditText)findViewById(R.id.ed_Blue);	
		edt_maDMC=(EditText)findViewById(R.id.ed_maDMC);
		edt_tenMau=(EditText)findViewById(R.id.ed_tenMau);
		tv_R=(TextView)findViewById(R.id.tv_showRed);
		tv_G=(TextView)findViewById(R.id.tv_showG);
		tv_B=(TextView)findViewById(R.id.tv_showB);
		Button btn_TraCuu=(Button)findViewById(R.id.btn_tracuu);
		Button btn_TimMau=(Button)findViewById(R.id.btn_timmau);
		Button btn_quaylai=(Button)findViewById(R.id.btn_quaylai);
		btn_TraCuu.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(traCuuDMC(edt_maDMC.getText().toString())){
					edt_tenMau.setText(tenMau);
					tv_R.setText(Red);
					tv_G.setText(Green);
					tv_B.setText(Blue);
					imv_mau.setBackgroundColor(Color.rgb(Integer.parseInt(Red),Integer.parseInt(Green), Integer.parseInt(Blue)));
				}
				else edt_tenMau.setText("Không tìm thấy màu phù hợp");
			}
		});
		btn_TimMau.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(timMauDMC()){
					edt_maDMC.setText(mamauDMC);
					edt_tenMau.setText(tenMau);
					traCuuDMC(mamauDMC);
					tv_R.setText(Red);
					tv_G.setText(Green);
					tv_B.setText(Blue);
					imv_mau.setBackgroundColor(Color.rgb(Integer.parseInt(Red),Integer.parseInt(Green), Integer.parseInt(Blue)));
					
				}
				else{
					edt_tenMau.setText("Kiểm tra lại tham số R, G, B");
				}
			}
		});
		btn_quaylai.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}
	public boolean traCuuDMC(String maTracuu){
		   mamauDMC="";tenMau="";
		    Red="";Green="";Blue="";
		   boolean ketqua=false;
	       try
	       {
	    	   InputStream is = getAssets().open("RGB2DMC.csv");
	       BufferedReader reader = new BufferedReader(new InputStreamReader(is));
	        String line;
	        
	       while ((line = reader.readLine()) != null )
	        {
	   String [] rowData = line.split(",");
	   if(rowData[0].equals(maTracuu)){
		   mamauDMC=rowData[0];
		   tenMau=rowData[1];
		   Red=rowData[2];
		   Green=rowData[3];
		   Blue=rowData[4];
		   break;
	   }

	                              
	   
	   }
	       if(line!=null){
	    		  ketqua=true;
	    	  }

	 
	      }
	       catch (FileNotFoundException e)
	       {
	  // TODO Auto-generated catch block
	  e.printStackTrace();
	  
	      } catch (IOException e) {
	  // TODO Auto-generated catch block
	  e.printStackTrace();
	 }
	       return ketqua;
	   }
	public boolean timMauDMC(){
		boolean ketqua=true;
		int R1=0,G1=0,B1=0,R2,G2,B2;
		String ma="",ten="";
		double minColorDistance=999999999;
		double colorDistance=minColorDistance;
		try{
			R1=Integer.parseInt(edt_Red.getText().toString());
			G1=Integer.parseInt(edt_Green.getText().toString());
			B1=Integer.parseInt(edt_Blue.getText().toString());
			
		}
		catch(Exception e){
			ketqua=false;
		}
		if(ketqua){
		try
	       {
	    	   InputStream is = getAssets().open("RGB2DMC.csv");
	       BufferedReader reader = new BufferedReader(new InputStreamReader(is));
	        String line;
	        
	       while ((line = reader.readLine()) != null )
	        {
	   String [] rowData = line.split(",");
	   
		  try{
			  R2=Integer.parseInt(rowData[2]);
		  
		   G2=Integer.parseInt(rowData[3]);
		   B2=Integer.parseInt(rowData[4]);
		   colorDistance=0.2126 *(R1-R2)*(R1-R2) + 0.7152 *(G1-G2)*(G1-G2) + 0.0722*(B1-B2)*(B1-B2);

		  }catch(Exception e){
			  
		  }
		if(colorDistance<minColorDistance){
	minColorDistance=colorDistance;
	 ma=rowData[0];
	   ten=rowData[1];
}
	                              
	   
	   }
	      mamauDMC=ma; 
	 tenMau=ten;
	      }
	       catch (FileNotFoundException e)
	       {
	  // TODO Auto-generated catch block
	  e.printStackTrace();
	  ketqua=false;
	      } catch (IOException e) {
	  // TODO Auto-generated catch block
	  e.printStackTrace();
	  ketqua=false;
	 }
		}
		return ketqua;
	}
}
